package net.jlibrary.invoice.action;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import net.jlibrary.common.CoreConstants;
import net.jlibrary.common.core.annotation.Token;
import net.jlibrary.common.core.entity.Pagination;
import net.jlibrary.common.core.entity.PaginationParam;
import net.jlibrary.common.project.domain.Group;
import net.jlibrary.invoice.action.vo.ElectronicInvoiceActionVo;
import net.jlibrary.invoice.domain.InvoiceType;
import net.jlibrary.invoice.service.IElectronicInvoiceManager;

@RequestMapping("/electronicInvoice")
@RestController
public class ElectronicInvoiceAction {
	@Autowired
	private IElectronicInvoiceManager iElectronicInvoiceManager;

	@RequestMapping(value = "/electronicAdminView", method = { RequestMethod.GET })
	public ModelAndView view() {
		return new ModelAndView("/electronic/electronicInvoiceAdminView");
	}

	// 获得销方列表
	@RequestMapping(value = "/electronicInvoiceAdminJsonList", method = { RequestMethod.POST })
	public Pagination lists(ElectronicInvoiceActionVo electronicInvoice, PaginationParam param) {
		Pagination pagination = iElectronicInvoiceManager.listsPage(electronicInvoice, param);
		return pagination;
	}

	// 销方详情
	@RequestMapping(value = "/electronicInvoiceAdminJsonDetail", method = { RequestMethod.GET })
	public ElectronicInvoiceActionVo detail(ElectronicInvoiceActionVo electronicInvoice) {
		electronicInvoice = iElectronicInvoiceManager.get(electronicInvoice);
		return electronicInvoice;
	}

	// 初始化销方信息
	@RequestMapping(value = "/electronicInvoiceAdminJsonAddFormInit", method = { RequestMethod.GET })
	public ElectronicInvoiceActionVo addFormInit() {
		ElectronicInvoiceActionVo electronicInvoice = new ElectronicInvoiceActionVo();
		return electronicInvoice;
	}

	// 保存销方信息
	@RequestMapping(value = "/electronicInvoiceAdminJsonSave", method = { RequestMethod.POST })
	@Token
	public ModelMap save(@ModelAttribute("vm") ElectronicInvoiceActionVo electronicInvoice, @Token String token,
			BindingResult result) {
		iElectronicInvoiceManager.save(electronicInvoice);
		ModelMap map = new ModelMap();
		map.put("success", true);
		return map;
	}

	// 修改销方信息
	@RequestMapping(value = "/electronicInvoiceAdminJsonUpdate", method = { RequestMethod.POST })
	@Token
	public ModelMap update(@ModelAttribute("vm") ElectronicInvoiceActionVo electronicInvoice, @Token String token,
			BindingResult result) {
		iElectronicInvoiceManager.update(electronicInvoice);
		ModelMap map = new ModelMap();
		map.put("success", true);
		return map;
	}

	// 删除销方信息
	@RequestMapping(value = "/electronicInvoiceAdminJsonDelete", method = { RequestMethod.POST })
	public ModelMap delete(Long id) {
		ElectronicInvoiceActionVo electronicInvoice = new ElectronicInvoiceActionVo();
		electronicInvoice.setId(id);
		iElectronicInvoiceManager.delete(electronicInvoice);
		ModelMap map = new ModelMap();
		map.put("success", true);
		return map;
	}

	// 恢复销方信息
	@RequestMapping(value = "/electronicInvoiceAdminJsonRenew", method = { RequestMethod.POST })
	public ModelMap renew(Long id) {
		ElectronicInvoiceActionVo electronicInvoice = new ElectronicInvoiceActionVo();
		electronicInvoice.setId(id);
		iElectronicInvoiceManager.renew(electronicInvoice);
		ModelMap map = new ModelMap();
		map.put("success", true);
		return map;
	}

	@RequestMapping(value = "/groupDataJson", method = { RequestMethod.GET })
	public List<ModelMap> deviceJsonData() {
		List<Group> groups = iElectronicInvoiceManager.groups();
		List<ModelMap> maps = new ArrayList<ModelMap>();
		for (Group group : groups) {
			ModelMap map = new ModelMap();
			map.put(CoreConstants.VIEW_SELECT_LABEL_VALUE, group.getId());
			map.put(CoreConstants.VIEW_SELECT_LABEL_TEXT, group.getFullName());
			maps.add(map);
		}
		return maps;
	}

	@RequestMapping(value = "/invoiceTypeDataJson", method = { RequestMethod.GET })
	public List<ModelMap> invoiceTypeDataJson() {
		List<InvoiceType> invoiceTypes = iElectronicInvoiceManager.invoiceType();
		List<ModelMap> maps = new ArrayList<ModelMap>();
		for (InvoiceType type : invoiceTypes) {
			ModelMap map = new ModelMap();
			map.put(CoreConstants.VIEW_SELECT_LABEL_VALUE, type.getCode());
			map.put(CoreConstants.VIEW_SELECT_LABEL_TEXT, type.getName());
			maps.add(map);
		}
		return maps;
	}

}
